package com.fanh.service.impl;


import com.fanh.common.api.CommonPage;
import com.fanh.common.util.CommonUtil;
import com.fanh.dao.GuideLessonDao;
import com.fanh.dto.GuideLessonResult;
import com.fanh.mapper.*;
import com.fanh.model.*;
import com.fanh.service.GuidelessonService;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;


@Service("guideLessonService")
public class GuideLessonServiceImpl implements GuidelessonService {

    private Logger LOGGER = LoggerFactory.getLogger(GuideLessonServiceImpl.class);

    @Autowired
    private GuideLessonDao guideLessonDao;
    @Autowired
    private GuideReadHistoryMapper guideReadHistoryMapper;
    @Autowired
    private GuideLessonMapper guideLessonMapper;
    @Autowired
    private GuidePayMapper guidePayMapper;


    @Override
    public CommonPage<GuideLessonResult> getGuideLessonList(String courseId, int pageNo, int PageSize) {
        PageHelper.startPage(pageNo,PageSize);
        List<GuideLessonResult>  list = guideLessonDao.LessonBycourseId(courseId);
        return CommonPage.restPage(list);
    }

    @Override
    public boolean checkLessonStudy(String userId, String lessonId) {
        GuideReadHistoryExample example = new GuideReadHistoryExample();
        example.createCriteria().andUserIdEqualTo(userId).andLessonIdEqualTo(lessonId);
        long count = guideReadHistoryMapper.countByExample(example);
        return count>0;
    }

    @Override
    public List getPayLesson(String userId, String courseId) {
        GuideLessonExample lessonExample = new GuideLessonExample();
        lessonExample.createCriteria().andCourseIdEqualTo(courseId);
        List<GuideLesson> lessons = guideLessonMapper.selectByExample(lessonExample);

        GuidePayExample payExample = new GuidePayExample();
        payExample.createCriteria().andUserIdEqualTo(userId).andCourseIdEqualTo(courseId);

        List<GuidePay> pays = guidePayMapper.selectByExample(payExample);
        List<GuideLesson> payLesson = new ArrayList<>();

        if(lessons == null || lessons.size()<1){
            return null;
        }
        if(payLesson ==null || payLesson.size()<1){
            return null;
        }
         for(GuidePay pay : pays) {
            //所有课程
            if(pay.getType().equals("1")) {  //TYPE_ALL
                //判断时间是否在范围内
                Date start = pay.getStartDate();
                Date end = pay.getEndDate();
                payLesson = lessons.stream().filter(lesson -> lesson.getLessonDate().after(start) && lesson.getLessonDate().before(end))
                         .collect(Collectors.toList());

            } else {//指定课程
                if(pay.getTimeScope().equals("3")) { //TIME_ALL
                    payLesson.addAll(lessons);
                    break;
                } else {
                    //判断时间是否在范围内
                    Date start = pay.getStartDate();
                    Date end = pay.getEndDate();

                  payLesson =lessons.stream().filter(lesson -> lesson.getLessonDate().after(start) && lesson.getLessonDate().before(end))
                          .collect(Collectors.toList());
                }
            }
        }
         if(payLesson == null || payLesson.size()<1){
             return  null;
         }
        return payLesson.stream().map(resultLesson -> resultLesson.getId()).collect(Collectors.toList());
    }

    @Override
    public int updateRead(String userId, String lessonId) {
        GuideLesson Lesson = guideLessonMapper.selectByPrimaryKey(lessonId);
        GuideReadHistory crh = new GuideReadHistory();
        crh.setId(CommonUtil.uuid());
        crh.setReadTime(new Date());
        crh.setCourseId(Lesson.getCourseId());
        crh.setLessonId(lessonId);
        crh.setUserId(userId);
        return  guideReadHistoryMapper.insert(crh);
     }
}
